package com.house365.core.thirdpart.auth;

import android.content.Context;
import android.text.TextUtils;

import com.house365.core.thirdpart.auth.dto.AccessToken;
import com.house365.core.util.store.SharedPreferenceException;
import com.house365.core.util.store.SharedPreferencesUtil;

/**
 * @author py
 * @date 2012-11-5
 * @comment
 */
public class Thirdpart {
	public static final int TYPE_SINA_WEIBO = 1;
	public static final int TYPE_QQ_WEIBO = 2;
	public static final int TYPE_QQ_COMMUNITY_LOGIN = 3;
	public static final int TYPE_SINA_LOGIN = 4;


	public static final String ACTION_THIRDPART_TYPE="ACTION_THIRDPART_TYPE";
	//sina微博相关url
	public static String SINA_URL_OAUTH2_ACCESS_AUTHORIZE = "https://api.weibo.com/oauth2/authorize";
	public static String SINA_SERVER=  "https://api.weibo.com/2/";
	public static String SINA_UPLOAD_URL=SINA_SERVER+ "statuses/upload.json";
	public static String SINA_UPDATE_URL=SINA_SERVER+ "statuses/update.json";
	
	//腾讯微博相关url
	public static String QQ_URL_OAUTH2_ACCESS_AUTHORIZE = "https://open.t.qq.com/cgi-bin/oauth2/authorize";
	public static String QQ_UPLOAD_URL="https://open.t.qq.com/api/t/add_pic";
	public static String QQ_UPDATE_URL="https://open.t.qq.com/api/t/add";
	
	//qq社区默认相关url
	private static String QQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url= "https://graph.qq.com/oauth2.0/authorize";
	
	private SharedPreferencesUtil sharedPrefUtil;

	private String KEY_PREFIX = "ACCESS_TOKEN_";

	private AppKeyInfo appKeyInfo;
	private Thirdpart(Context context) {
		sharedPrefUtil = new SharedPreferencesUtil(
				context.getSharedPreferences("weibo", Context.MODE_PRIVATE));
	}

	private static Thirdpart instance;

	public static Thirdpart getInstace(Context context) {
		if (instance == null) {
			instance = new Thirdpart(context);
		}
		return instance;
	}

	public AppKeyInfo getAppKeyInfo() {
		return appKeyInfo;
	}

	public void setAppKeyInfo(AppKeyInfo appKeyInfo) {
		this.appKeyInfo = appKeyInfo;
	}
	
	public void setQQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url(
			String qQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url) {
		QQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url = TextUtils.isEmpty(qQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url) 
				? QQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url
				: qQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url;
	}

	public boolean isBind(int type) {
		AccessToken accessToken;
		try {
			accessToken = (AccessToken) sharedPrefUtil.getObject(KEY_PREFIX
					+ type, AccessToken.class);
		} catch (SharedPreferenceException e) {
			e.printStackTrace();
			accessToken = null;
		}
		if (accessToken != null && accessToken.isSessionValid()) {
			return true;
		} else {
			return false;
		}
	}
	public void unBind(int type){
		sharedPrefUtil.clean(KEY_PREFIX
					+ type);
	}
	public void bind(int type, AccessToken accessToken) {
		sharedPrefUtil.putObject(KEY_PREFIX + type, accessToken);
	}
	public String getAuthUrl(int type){
		if(type == TYPE_SINA_WEIBO){
			return SINA_URL_OAUTH2_ACCESS_AUTHORIZE;
		}else if(type == TYPE_QQ_WEIBO){
			return QQ_URL_OAUTH2_ACCESS_AUTHORIZE;
		}else if(type == TYPE_QQ_COMMUNITY_LOGIN) {
			return QQ_COMM_URL_OAUTH2_ACCESS_AUTHORIZE_url;
		}
		
		return SINA_URL_OAUTH2_ACCESS_AUTHORIZE;
	}
	public String getUploadUrl(int type){
		if(type == TYPE_SINA_WEIBO){
			return SINA_UPLOAD_URL;
		}else if(type == TYPE_QQ_WEIBO){
			return QQ_UPLOAD_URL;
		}
		return SINA_UPLOAD_URL;
	}
	public String getUpdateUrl(int type){
		if(type == TYPE_SINA_WEIBO){
			return SINA_UPDATE_URL;
		}else if(type == TYPE_QQ_WEIBO){
			return QQ_UPDATE_URL;
		}
		return SINA_UPDATE_URL;
	}
	public AccessToken getAccessToken(int type){
		AccessToken accessToken=null;
		try {
			accessToken = (AccessToken) sharedPrefUtil.getObject(KEY_PREFIX
					+ type, AccessToken.class);
		} catch (SharedPreferenceException e) {
			e.printStackTrace();
			accessToken = null;
		}
		return accessToken;
	}
}
